package zhongbo.java.air.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.mapper.AirAlertLogMapper;
import zhongbo.java.air.monitor.model.AirAlertLog;
import zhongbo.java.air.monitor.service.IAirAlertLogService;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName AirAlertLogService
 * @Description TODO
 * @Author 琪
 * @Date 2025/4/9 09:43
 */
@Service
@Slf4j
public class AirAlertLogServiceImpl implements IAirAlertLogService {

    @Autowired
    private AirAlertLogMapper airAlertLogMapper;

    @Override
    public AirAlertLog add(AirAlertLog alertLog) {
        validAirAlertLog(alertLog);
        List<AirAlertLog> alertLogs = airAlertLogMapper.selectList(null);
        String code = alertLog.getIcaoCode();
        for (AirAlertLog a : alertLogs) {
            if (Objects.equals(code, a.getIcaoCode())) {
                throw new RuntimeException("ICAO不能重复");
            }
        }
        alertLog.setCreateTime(new Date());
        alertLog.setUpdateTime(new Date());

        int insert = airAlertLogMapper.insert(alertLog);
        if (insert > 0) {
            return alertLog;
        }

        throw new RuntimeException("添加告警日志失败");
    }

    private void validAirAlertLog(AirAlertLog alertLog) {
        if (alertLog == null) {
            throw new RuntimeException("信息不能为空");
        }
        String code = alertLog.getIcaoCode();
        if (code.length() > 6) {
            throw new RuntimeException("ICAO不能不能超过6个字段");
        }
    }

    @Override
    public String delete(String id) {
        if (id == null) {
            throw new RuntimeException("id必须填写");
        }
        int delete = airAlertLogMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        throw new RuntimeException("删除告警日志失败");
    }

    @Override
    public AirAlertLog edit(AirAlertLog alertLog1) {
        validAirAlertLog(alertLog1);
        String id = alertLog1.getId();
        if (id == null) {
            throw new RuntimeException("id必须填写");
        }
        findOne(id);
        alertLog1.setUpdateTime(new Date());
        int edit = airAlertLogMapper.updateById(alertLog1);
        if (edit > 0) {
            return alertLog1;
        }
        throw new RuntimeException("修改告警日志失败");

    }

    @Override
    public AirAlertLog findOne(String id) {
        if (id == null) {
            throw new RuntimeException("id必须填写");
        }
        AirAlertLog airAlertLog = airAlertLogMapper.selectById(id);
        if (airAlertLog == null) {
            throw new RuntimeException("此条告警日志不存在");
        }
        return airAlertLog;
    }

    @Override
    public PageVo<AirAlertLog> findPage(String keyword, String fields,
                                        String icao, String startDate, String endDate,
                                        Integer PageNumber, Integer PageSize,
                                        String orderByField, Boolean orderByAsc) {
        QueryWrapper<AirAlertLog> queryWrapper = new QueryWrapper<>();
        if (icao != null) {
            queryWrapper.eq("icao_code", icao);
        }
        if (startDate != null && endDate != null) {
                queryWrapper.between("trigger_time", startDate, endDate);
        }
        //支持指定字段模糊搜索Keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.like(StrUtil.toUnderlineCase(field), keyword);
            }

        }
        //检查是否有排序
        if (StrUtil.isNotBlank(orderByField)) {
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                queryWrapper.orderByAsc(orderByField);
            } else {
                queryWrapper.orderByAsc(orderByField);
            }
        }
        Page<AirAlertLog> pageParam = new Page<>(PageNumber, PageSize);
        Page<AirAlertLog> page = airAlertLogMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(page);
    }
}
